<script setup>
import { useRouter } from 'vue-router';
import { ref, computed } from 'vue';
import { showToast } from 'vant';
import { setPasswordFn } from '@/api/register';

const router = useRouter()
const password = ref('')
const password2 = ref('')

const flag = computed(() => {
    return !(password.value.length >= 6 && password.value == password2.value)
})

function onSubmit() {
    setPasswordFn({
        password: password.value
    }).then(res => {
        // console.log(res);
        showToast(res.message)
        if (res.data.userInfo) {
            router.push('/login')
        } else {
            router.push('/register/step1')
        }
    })
}

function toback() {
    router.back()
}
</script>

<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field v-model="password" name="密码" label="密码" placeholder="请输入至少6位密码"
                :rules="[{ required: true, message: '请填写至少6位的密码' }]" type="password" />
            <van-field v-model="password2" name="确认密码" label="确认密码" placeholder="请再次输入密码"
                :rules="[{ required: true, message: '请确认密码' }]" type="password" />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="danger" native-type="submit" :disabled="flag">
                下一步
            </van-button>
        </div>
    </van-form>
    <div style="margin: 16px;">
        <van-button round block type="success" native-type="submit" @click="toback">
            返回上一步
        </van-button>
    </div>
</template>